package com.eastfair.home.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.home.dto.ReportVenueDrawingPageQuery;
import com.eastfair.home.dto.ReportVenueDrawingSaveDTO;
import com.eastfair.home.dto.ReportVenueDrawingUpdateDTO;
import com.eastfair.home.entity.ReportVenueDrawing;
import com.eastfair.home.service.HomeReportVenueDrawingAuditHistoryService;
import com.eastfair.home.service.HomeReportVenueDrawingCategoryAuditHistoryService;
import com.eastfair.home.service.ReportVenueDrawingService;
import com.eastfair.home.vo.ReportVenueDrawingCategoryVO;
import com.eastfair.home.vo.ReportVenueDrawingVO;
import com.eastfair.venueservice.dto.ReportVenueDrawingCategoryAuditHistoryDTO;
import com.eastfair.venueservice.vo.ReportVenueDrawingAuditHistoryVO;
import com.eastfair.venueservice.vo.ReportVenueDrawingCategoryAuditHistoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 场馆一键报馆图纸
 * </p>
 *
 * @author dq
 * @date 2022-09-07
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/reportVenueDrawing")
@Api(value = "ReportVenueDrawing", tags = "场馆一键报馆图纸")
@PreAuth(replace = "home:reportVenueDrawing:")
public class ReportVenueDrawingController extends SuperController<ReportVenueDrawingService, Long, ReportVenueDrawing, ReportVenueDrawingPageQuery, ReportVenueDrawingSaveDTO, ReportVenueDrawingUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private ReportVenueDrawingService reportVenueDrawingService;

    @Resource
    private HomeReportVenueDrawingCategoryAuditHistoryService homeReportVenueDrawingCategoryAuditHistoryService;

    @Resource
    private HomeReportVenueDrawingAuditHistoryService homeReportVenueDrawingAuditHistoryService;

    @Override
    public void handlerResult(IPage<ReportVenueDrawing> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ReportVenueDrawing> reportVenueDrawingList = list
                .stream()
                .map((map) -> ReportVenueDrawing.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(reportVenueDrawingList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ReportVenueDrawingPageQuery> params) {
        ReportVenueDrawingPageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 一键报馆编号查询图纸分类VO列表.
     *
     * @param reportVenueSpaceNumber the report venue space number
     * @return the r
     */
    @ApiOperation("一键报馆编号查询图纸分类VO列表")
    @GetMapping("/reportVenueDrawingCategory/{reportVenueSpaceNumber}")
    public R<List<ReportVenueDrawingCategoryVO>> queryCategoryByReportVenueSpaceNumber(@PathVariable("reportVenueSpaceNumber") String reportVenueSpaceNumber) {
        log.info("queryCategoryByReportVenueSpaceNumber - 一键报馆编号查询图纸分类VO列表, reportVenueSpaceNumber={}", reportVenueSpaceNumber);
        return R.successDef(reportVenueDrawingService.listCategoryByReportVenueSpaceNumber(reportVenueSpaceNumber));
    }

    /**
     * 一键报馆编号查询图纸VO列表.
     *
     * @param reportVenueSpaceNumber the report venue space number
     * @return the r
     */
    @ApiOperation("一键报馆编号查询图纸VO列表")
    @GetMapping("/reportVenueDrawing")
    public R<List<ReportVenueDrawingVO>> queryByReportVenueSpaceNumber(@RequestParam("reportVenueSpaceNumber") String reportVenueSpaceNumber,
                                                                       @RequestParam("drawingCategoryId") Long drawingCategoryId) {
        log.info("queryByReportVenueSpaceNumber - 一键报馆编号查询图纸VO列表, reportVenueSpaceNumber={}", reportVenueSpaceNumber);
        return R.successDef(reportVenueDrawingService
                .listByReportVenueSpaceNumber(reportVenueSpaceNumber, drawingCategoryId));
    }

    /**
     * 报馆号查询图纸分类审核VO历史列表.
     *
     * @param pageParams the page params
     * @return the r
     */
    @ApiOperation("报馆号查询图纸分类审核VO历史列表")
    @PostMapping("/query/history")
    public R<IPage<ReportVenueDrawingCategoryAuditHistoryVO>> queryHistory(@RequestBody PageParams<ReportVenueDrawingCategoryAuditHistoryDTO> pageParams) {
        log.info("queryHistory - 报馆号查询图纸分类审核VO历史列表, pageParams={}", pageParams);
        return R.successDef(homeReportVenueDrawingCategoryAuditHistoryService.queryHistory(pageParams));
    }

    /**
     * 图纸分类审核历史ID查询图纸历史列表.
     *
     * @param reportVenueDrawingCategoryAuditHistoryId the report venue drawing category audit history id
     * @return the r
     */
    @ApiOperation("图纸分类审核历史ID查询图纸历史列表")
    @GetMapping("/categoryAuditHistoryId/{reportVenueDrawingCategoryAuditHistoryId}")
    public R<List<ReportVenueDrawingAuditHistoryVO>> queryByReportVenueDrawingCategoryAuditHistoryId(@PathVariable("reportVenueDrawingCategoryAuditHistoryId")
                                                                                              Long reportVenueDrawingCategoryAuditHistoryId) {
        log.info("queryByReportVenueDrawingCategoryAuditHistoryId - 图纸分类审核历史ID查询图纸历史列表, reportVenueDrawingCategoryAuditHistoryId={}", reportVenueDrawingCategoryAuditHistoryId);
        List<ReportVenueDrawingAuditHistoryVO> list
                = homeReportVenueDrawingAuditHistoryService.queryByReportVenueDrawingCategoryAuditHistoryId(reportVenueDrawingCategoryAuditHistoryId);
        return R.successDef(list);
    }

    /**
     * 图纸url删除图纸.
     *
     * @param list the list
     * @return the r
     */
    @ApiOperation("图纸url删除图纸")
    @PostMapping("/remove/imageUrl")
    public R<Boolean> deleteByImageUrl(@RequestBody List<String> list) {
        log.info("deleteByImageUrl - 图纸url删除图纸, list={}", list);
        return R.successDef(reportVenueDrawingService.removeByImageUrl(list));
    }
}
